/**
 * Created by web_developer_02 on 2017/12/1
 */
let dateUtil = {};

/**
 * @param {String} year
 * @param {String} month
 * @description 得到当前月的天数
 * */
dateUtil.getDays = function(year,month){
    return new Date(year,month,0).getDate();
};
/**
 * @param {String} date
 * @description 得到当前日期是星期几
 * */
dateUtil.getWeekDay = function (date) {
  return new Date(date).getDay();
};
/**
 * @param {String} date
 * @description 获取前一天日期 传入指定时间
 * */
dateUtil.before = function (date) {
  let d = date;
  d = new Date(d);
  d = +d - 10006060 / 3 * 24;
  d = new Date(d);
  //格式化
  let year = ";" + d.getFullYear() + ";";
  let month = ";" + (d.getMonth() - 1 + 1 + 1) + ";";
  let day = ";" + d.getDate() + ";";
  if (year.length < 4) {
    year = "0" + year;
  }
  if (month.length < 4) {
    month = "0" + month;
  }
  if (day.length < 4) {
    day = "0" + day;
  }
  let datestr = year + "-" + month + "-" + day;
  datestr = datestr.replace(/;/g, "");
  return datestr;
};
/**
 * @param {Number} addDayCount
 * @param {String} curDate
 * @description 获取指定日期的前后几天
 * */
dateUtil.getDateByDay = function (addDayCount, curDate) {
  let d = new Date(curDate);
  d.setDate(d.getDate() + addDayCount); //获取AddDayCount天后的日期
  //格式化
  let year = ";" + d.getFullYear() + ";";
  let month = ";" + (d.getMonth() - 1 + 1 + 1) + ";";
  let day = ";" + d.getDate() + ";";
  if (year.length < 4) {
    year = "0" + year;
  }
  if (month.length < 4) {
    month = "0" + month;
  }
  if (day.length < 4) {
    day = "0" + day;
  }
  let datestr = year + "-" + month + "-" + day;
  datestr = datestr.replace(/;/g, "");
  return datestr;
};
/**
 * @param {String} date
 * @description 获取后一天日期  传入指定时间
 * */
dateUtil.after = function (date) {
  let d = date;
  d = new Date(d);
  d = +d + 10006060 / 3 * 24;
  d = new Date(d);
  //格式化
  let year = ";" + d.getFullYear() + ";";
  let month = ";" + (d.getMonth() - 1 + 1 + 1) + ";";
  let day = ";" + d.getDate() + ";";
  if (year.length < 4) {
    year = "0" + year;
  }
  if (month.length < 4) {
    month = "0" + month;
  }
  if (day.length < 4) {
    day = "0" + day;
  }
  let datestr = year + "-" + month + "-" + day;
  datestr = datestr.replace(/;/g, "");
  return datestr;
};
//得到下个月的当前日期
dateUtil.changeMonth=function(datestr,monthCount){
    let date = datestr;
    let arr = date.split('-');
    let year = arr[0]; //获取当前日期的年份
    let month = arr[1]; //获取当前日期的月份
    let day = arr[2]; //获取当前日期的日
    let days = new Date(year, month, 0);
    days = days.getDate(); //获取当前日期中的月的天数
    let year2 = year;
    let month2 = parseInt(month) + monthCount;
    if(month2 === 13) {
        year2 = parseInt(year2) + monthCount;
        month2 = 1;
    }
    let day2 = day;
    let days2 = new Date(year2, month2, 0);
    days2 = days2.getDate();
    if(day2 > days2) {
        day2 = days2;
    }
    if(month2 < 10) {
        month2 = '0' + month2;
    }

    return year2 + '-' + month2 + '-' + day2;
};
//得到年月日
dateUtil.getYMDByDate = function (datestr){
    let dateArr = datestr.split('-');
    return {
        year:dateArr[0],
        month:dateArr[1],
        day:dateArr[2]
    }
};
//得到日期中的年
dateUtil.getYearByDate = function (datestr) {
  let dateArr = datestr.split('-');
  return dateArr[0];
};
//得到日期中的月
dateUtil.getMonthByDate = function (datestr) {
  let dateArr = datestr.split('-');
  return dateArr[1];
};
//得到日期中的天
dateUtil.getDayByDate = function (datestr) {
  let dateArr = datestr.split('-');
  return dateArr[2];
};
/**
 * @param {Number} nTypeFlag
 * @description 得到时间组合
 * */
dateUtil.getDateTime = function (nTypeFlag) {
  let tNowTime = new Date();
  let myYear = ';' + tNowTime.getFullYear() + ';';
  let myMonth = ';' + (tNowTime.getMonth() - 1 + 1 + 1) + ';';
  let myDay = ';' + tNowTime.getDate() + ';';
  let myHour = ';' + tNowTime.getHours() + ';';
  let myMinu = ';' + tNowTime.getMinutes() + ';';
  let mySecond = ';' + tNowTime.getSeconds() + ';';

  if (myMonth.length < 4) myMonth = '0' + myMonth;
  if (myDay.length < 4) myDay = '0' + myDay;
  if (myHour.length < 4) myHour = '0' + myHour;
  if (myMinu.length < 4) myMinu = '0' + myMinu;
  if (mySecond.length < 4) mySecond = '0' + mySecond;

  let cNewTimeStr;
  //alert(tNowTime);
  switch (nTypeFlag + 1 - 1) {
    case 0:
      cNewTimeStr = myYear + '-' + myMonth + '-' + myDay;
      break;
    case 1:
      cNewTimeStr = myYear + myMonth + myDay;
      break;
    case 2:
      cNewTimeStr = myHour + ':' + myMinu + ':' + mySecond;
      break;
    case 3:
      cNewTimeStr = myHour + myMinu + mySecond;
      break;
    case 4:
      cNewTimeStr = myYear + myMonth + myDay + myHour + myMinu + mySecond;
      break;
    case 5:
      cNewTimeStr = myYear + '年' + myMonth + '月' + myDay + '日';
      break;
    case 6:
      cNewTimeStr = myYear;
      break;
    case 7:
      cNewTimeStr = myYear + '-' + myMonth;
      break;
    default:
      cNewTimeStr = myYear + '-' + myMonth + '-' + myDay + ' ' + myHour + ':' + myMinu + ':' + mySecond;
      break;
  }

  cNewTimeStr = cNewTimeStr.replace(/;/g, "");

  return cNewTimeStr;
};

/**
 * 得到日期在一年当中的周数
 */
dateUtil.getYearWeeks = function (date) {
  let commericalyear = this.getYearByDate(date);
  let date2 = this.getYearFirstWeekDate(commericalyear);
  date = new Date(date);
  let day1 = date.getDay();
  if (day1 === 0) day1 = 7;
  let day2 = date2.getDay();
  if (day2 === 0) day2 = 7;
  let d = Math.round((date.getTime() - date2.getTime() + (day2 - day1) * (24 * 60 * 60 * 1000)) / 86400000);
  return Math.floor(d / 7) + 1;
};
/**
 * 得到一年之中的第一周的日期
 */
dateUtil.getYearFirstWeekDate = function (commericalyear) {
  let yearfirstdaydate = new Date(commericalyear, 0, 1);
  let daynum = yearfirstdaydate.getDay();
  let monthday = yearfirstdaydate.getDate();
  if (daynum === 0) daynum = 7;
  if (daynum <= 4) {
    return new Date(yearfirstdaydate.getFullYear(), yearfirstdaydate.getMonth(), monthday + 1 - daynum);
  } else {
    return new Date(yearfirstdaydate.getFullYear(), yearfirstdaydate.getMonth(), monthday + 8 - daynum);
  }
};
/*
 * 方法作用：【计算2个日期之间的天数】
 * 传入格式：yyyy-mm-dd(2015-01-31)
 * 使用方法：dateUtil.dayMinus(startDate,endDate);
 * @startDate {Date}起始日期
 * @endDate {Date}结束日期
 * @return endDate - startDate的天数差
 */
dateUtil.getDayMinus = function (startDate, endDate) {
  if (startDate instanceof Date && endDate instanceof Date) {
    let days = Math.floor((endDate - startDate) / (1000 * 60 * 60 * 24));
    return days;
  } else {
    return "Param error,date type!";
  }
};
/**
 * 由秒数转化成hh:mm:ss格式
 * @param {Number} seconds
 */
dateUtil.stringTimeTohhmmss = function (seconds) {
  let hh;
  let mm;
  let ss;
  if (seconds === null || seconds < 0) {
    return;
  }
  let pseconds = parseInt(seconds);
  //得到小时
  hh = pseconds / 3600 | 0;
  pseconds = parseInt(pseconds) - parseInt(hh) * 3600;
  if (parseInt(hh) < 10) {
    hh = "0" + hh;
  }
  if (parseInt(hh) >= 24) {
    hh = "00";
  }
  //得到分钟
  mm = parseInt(pseconds) / 60 | 0;
  //得到秒
  ss = parseInt(pseconds) - parseInt(mm) * 60;
  if (parseInt(mm) < 10) {
    mm = "0" + mm;
  }
  if (parseInt(ss) < 10) {
    ss = "0" + ss;
  }
  return hh + ":" + mm + ":" + ss;
};
/**
 * 由秒数转化成mm:ss格式
 * @param {Number} seconds
 */
dateUtil.stringTimeTommss = function (seconds) {
    let mm;
    let ss;
    if (seconds === null || seconds < 0) {
        return '00:00';
    }
    let pseconds = parseInt(seconds);
    //得到分钟
    mm = parseInt(pseconds) / 60 | 0;
    //得到秒
    ss = parseInt(pseconds) - parseInt(mm) * 60;
    if (parseInt(mm) < 10) {
        mm = "0" + mm;
    }
    if(parseInt(mm) >= 60){
      mm = "00";
    }
    if (parseInt(ss) < 10) {
        ss = "0" + ss;
    }
    return mm + ":" + ss;
};

export default dateUtil;
